package org.skydiveapplibrary.ui;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;

import android.content.Context;

public class FormatUtility
{
	// formats
	private static final NumberFormat numericFormat = NumberFormat.getNumberInstance();
	private static final NumberFormat floatFormat = NumberFormat.getNumberInstance();
	
	static
	{
		floatFormat.setMaximumFractionDigits(2);
	}
	
	public static boolean isEmptyOrNull(String str)
	{
		return (str == null || str.equals(""));
	}
	
	public static String toDateString(Context context, Date date)
	{
		DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM);
		return format.format(date);
	}
	
	public static String toNumericString(int number)
	{
		return numericFormat.format(number);
	}
	
	public static int fromNumericString(String string)
	{
		if (isEmptyOrNull(string))
		{
			return 0;
		}
		
		try
		{
			return numericFormat.parse(string).intValue();
		}
		catch (ParseException pe)
		{
			return 0;
		}
	}
	
	public static Integer toInteger(String intString, int defaultValue)
	{
		if (isEmptyOrNull(intString))
			return defaultValue;
		
		try
		{
			return numericFormat.parse(intString).intValue();
		}
		catch (ParseException pe)
		{
			return defaultValue;
		}
	}
	
	public static String toNumericString(float number)
	{
		return floatFormat.format(number);
	}
	
	public static Float toFloat(String floatString, float defaultValue)
	{
		if (isEmptyOrNull(floatString))
			return defaultValue;
		
		try
		{
			return floatFormat.parse(floatString).floatValue();
		}
		catch (ParseException pe)
		{
			return defaultValue;
		}
	}
}
